/*
* @Author: Rosen
* @Date:   2018-02-05 13:01:18
* @Last Modified by:   Rosen
* @Last Modified time: 2018-02-05 13:24:59
*/
import React        from 'react';
import { Link }     from 'react-router-dom';
import MUtil        from 'util/mm.jsx'
import Order        from 'service/order-service.jsx'

import PageTitle    from 'component/page-title/index.jsx';
import ListSearch   from './index-list-search.jsx';
import TableList    from 'util/table-list/index.jsx';
import Pagination   from 'util/pagination/index.jsx';


const _mm           = new MUtil();
const _order        = new Order();

class OrderList extends React.Component{
    constructor(props){
        super(props);
        this.state = {
            list            : [],
            pageNum         : 1,
            listType        : 'list' // list / search
        };
    }
    componentDidMount(){
        this.loadOrderList();
    }
    // 加载商品列表
    loadOrderList(){
        let listParam = {};
        listParam.listType = this.state.listType;
        listParam.pageNum  = this.state.pageNum;
        // 如果是搜索的话，需要传入搜索类型和搜索关键字
        if(this.state.listType === 'search'){
                 listParam.searchType = this.state.searchType;
                  listParam.keyword    = this.state.searchKeyword;
        }

        _order.getOrderList(listParam).then(res => {  //请求接口失败，继续
            this.setState(res);
        }, errMsg => {
            this.setState({
                list : []
            });
            _mm.errorTips(errMsg);
        });
  

    }




    // 搜索
    onSearch(searchType, searchKeyword){
         console.log(searchType);  
        console.log(searchKeyword);
        let listType = searchKeyword === '' ? 'list' : 'search';
        this.setState({
            listType        : listType,
            pageNum         : 1,
            searchType      : searchType,
            searchKeyword   : searchKeyword
        }, () => {
            this.loadOrderList();
        });
    }
    // 页数发生变化的时候
    onPageNumChange(pageNum){
        this.setState({
            pageNum : pageNum
        }, () => {
            this.loadOrderList();
        });
    }

      // 更新快递的名字
    onUpdateName(orderNo, expressage){
        let newName = window.prompt('请输入新的快递名称', expressage);

         if(newName.match( /^[0-9]+$/ )){
         _mm.errorTips("快递公司怎么会有数字？");

         }

         else if(newName.length ===0){
           _mm.errorTips("不能为空！");
            
            }
          else{
              _order.updateExpressageName({
                orderNo:orderNo,
                expressage : newName
            }).then(res => {
                _mm.successTips(res);
                this.loadOrderList();
            }, errMsg => {
                _mm.errorTips(errMsg);
            });
        }
    }
        // 更新快递单号
    onUpdateNo(orderNo, expressage){
        let newName = window.prompt('请输入新的单号', expressage);
    let checkResult=this.checkLoginInfo(newName);
            if(checkResult.status){ //如果返回为真，
              _order.updateExpressageNo({
                orderNo:orderNo,
                expreNumber : newName
            }).then(res => {
                _mm.successTips(res);
                this.loadOrderList();
            }, errMsg => {
                _mm.errorTips(errMsg);
            });
        }// 验证不通过
        else{
            _mm.errorTips(checkResult.msg);
        }
        
    }

    checkLoginInfo(newname){
  let name = $.trim(newname);  
        // 判断为空
        if(name.length ===0){
            return {
                status: false,
                msg: '不能为空！'
            }
        }
        // 判断密码为空
        if(/^[0-9]+\.?[0-9]+?$/.test(name)){
            return {
                status: true,
                msg: '验证通过'
            }
        }
        return {
            status : false,
            msg : '单号全部应是数字！'
        }
    }
    render(){
        let tableHeads = ['订单号', '收件人', '订单状态', '订单总价','图片上传与否', '快递公司','单号','创建时间', '操作'];
        return (
            <div id="page-wrapper">
                <PageTitle title="订单列表" />
                <ListSearch onSearch={(searchType, searchKeyword) => {this.onSearch(searchType, searchKeyword)}}/>
                <TableList tableHeads={tableHeads}>
                    {
                        this.state.list.map((order, index) => {
                            return (
                                <tr key={index}>
                                    <td>
                                        <Link to={ `/order/detail/${order.orderNo}` }>{order.orderNo}</Link>
                                    </td>
                                    <td>{order.receiverName}</td>
                                    <td>{order.statusDesc}</td>
                                    <td>￥{order.payment}</td>
                                     <td>{order.uploadStatus}</td>  
                                      <td>  
                                 <a className="opear"
                            onClick={(e) => this.onUpdateName(order.orderNo, order.expressage)}>{order.expressage}</a>
                                      </td>
                                        <td>
                                            <a className="opear"
                            onClick={(e) => this.onUpdateNo(order.orderNo, order.expreNumber)}>{order.expreNumber}</a>
                                        </td>
                                    <td>{order.createTime}</td>
                                    <td>
                                        <Link to={ `/order/detail/${order.orderNo}` }>详情</Link>
                                    </td>
                                </tr>
                            );
                        })
                    }
                </TableList>
                <Pagination current={this.state.pageNum} 
                    total={this.state.total} 
                    onChange={(pageNum) => this.onPageNumChange(pageNum)}/>
            </div>
        );
    }
}

export default OrderList;